﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace GT.UI.WinForms
{
    public static partial class Extensions
    {
        public static void SetAllowedKeys(this Component textbox, string regexPattern)
        {
            Action<object, KeyPressEventArgs> KeyPress = (src, ev) =>
            {
                if (!ev.KeyChar.ToString().Like(regexPattern))
                {
                    ev.KeyChar = '\0';
                    ev.Handled = true;
                }
            };

            if (textbox is TextBox)
                (textbox as TextBox).KeyPress += (src, ev) => KeyPress(src, ev);
            else if (textbox is ToolStripTextBox)
                (textbox as ToolStripTextBox).KeyPress += (src, ev) => KeyPress(src, ev);
            else if (textbox is RichTextBox)
                (textbox as RichTextBox).KeyPress += (src, ev) => KeyPress(src, ev);
        }
    }
}
